{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# “一站式”因子探索性分析 (Factor EDA) - v2.0\n",
    "\n",
    "本notebook完全基于`ResearchContext`和`AlphaDataTool`，提供一个与AlphaHome主系统无缝集成的、现代化的因子分析流程。\n",
    "\n",
    "**核心流程:**\n",
    "1. **初始化 `ResearchContext`**: 作为所有数据和服务的统一入口。\n",
    "2. **执行研究流水线**: 复用`main.py`中定义的自动化流程，直接得到计算好的因子和未来收益率。\n",
    "3. **交互式分析与可视化**: 对流水线产出的结果进行更深入、灵活的探索。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入必要的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "from pathlib import Path\n",
    "import sys\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# 设置中文字体和图表风格\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "sns.set_style('whitegrid')\n",
    "\n",
    "# 添加项目根目录到Python路径，以便导入alphahome模块\n",
    "# 假设此notebook在 '.../research/projects/your_project/notebooks/' 下运行\n",
    "project_root = Path.cwd().parents[3]\n",
    "if str(project_root) not in sys.path:\n",
    "    sys.path.insert(0, str(project_root))\n",
    "    print(f\"项目根目录已添加: {project_root}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 初始化与数据加载\n",
    "\n",
    "我们将通过`ResearchContext`来获取所有需要的服务和数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from research.tools.context import ResearchContext\n",
    "from main import create_factor_research_pipeline\n",
    "\n",
    "# 初始化 ResearchContext\n",
    "# research project的根目录是notebooks目录的上一级\n",
    "project_path = Path.cwd().parent\n",
    "context = ResearchContext(project_path=project_path)\n",
    "\n",
    "# 检查配置是否加载成功\n",
    "print(\"研究项目配置加载成功:\")\n",
    "display(context.config['project'])\n",
    "\n",
    "# 使用 data_tool 获取股票列表\n",
    "stock_list = context.data_tool.get_stock_list()[:20] # 选择20只股票作为示例\n",
    "print(f\"\\n成功获取 {len(stock_list)} 只股票用于分析。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 执行自动化研究流水线\n",
    "\n",
    "我们将直接复用 `main.py` 中定义的 `create_factor_research_pipeline` 函数来创建并运行流水线。这个流水线会自动完成数据加载、因子计算、未来收益率计算等所有预处理工作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建流水线\n",
    "pipeline = create_factor_research_pipeline(context)\n",
    "\n",
    "# 获取流水线初始参数 (来自config.yml)\n",
    "initial_params = context.config.get('pipeline_params', {})\n",
    "initial_params['stock_list'] = stock_list # 使用我们刚刚选择的股票列表\n",
    "\n",
    "# 运行流水线\n",
    "pipeline.run(initial_params=initial_params)\n",
    "\n",
    "# 从流水线结果中提取最终的、包含所有因子的DataFrame\n",
    "final_result = pipeline.results[-1]['output']\n",
    "df_final = final_result.get('factor_data')\n",
    "\n",
    "print(\"自动化流水线执行完成！\")\n",
    "if df_final is not None:\n",
    "    print(f\"最终数据集包含 {len(df_final)} 条记录，{len(df_final.columns)} 个字段。\")\n",
    "    display(df_final.head())\n",
    "else:\n",
    "    print(\"最终数据集为空，请检查流水线各步骤的输出。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 交互式因子分析\n",
    "\n",
    "现在，我们已经有了一个干净、完整的数据集 `df_final`，其中包含了原始价格、计算好的因子和未来收益率。接下来，我们可以像之前一样，进行深入的交互式分析。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if df_final is not None and not df_final.empty:\n",
    "    from research.templates.database_research.src.steps import FactorICAnalysisStep, FactorQuantileAnalysisStep\n",
    "\n",
    "    # 提取因子列\n",
    "    factor_cols = list(context.config.get('factors', {}).keys())\n",
    "\n",
    "    print(\"开始对以下因子进行交互式分析:\")\n",
    "    print(factor_cols)\n",
    "else:\n",
    "    print(\"数据为空，跳过交互式分析。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 因子IC分析 (复现流水线步骤)\n",
    "\n",
    "我们可以手动调用分析步骤，对单个或多个因子进行分析。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if df_final is not None and not df_final.empty:\n",
    "    ic_step = FactorICAnalysisStep(context, factor_cols=factor_cols)\n",
    "    ic_step.run(factor_data=df_final)\n",
    "\n",
    "    # 也可以从上下文中获取之前流水线运行的结果\n",
    "    ic_results = context.get_analysis_result('ic_analysis')\n",
    "    print(\"\\nIC分析结果摘要:\")\n",
    "    for factor, result in ic_results.items():\n",
    "        print(f\"- 因子: {factor}, IC均值: {result['IC均值']:.4f}, ICIR: {result['ICIR']:.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 因子分位数分析 (复现流水线步骤)\n",
    "\n",
    "手动调用分位数分析步骤，可以方便地更换要分析的因子。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if df_final is not None and not df_final.empty:\n",
    "    # 对 'ma_5' 因子进行5分位分析\n",
    "    if 'ma_5' in factor_cols:\n",
    "        quantile_step_ma5 = FactorQuantileAnalysisStep(context, factor_col='ma_5', quantiles=5)\n",
    "        quantile_step_ma5.run(factor_data=df_final)\n",
    "\n",
    "    # 对 'rsi_14' 因子进行10分位分析\n",
    "    if 'rsi_14' in factor_cols:\n",
    "        quantile_step_rsi14 = FactorQuantileAnalysisStep(context, factor_col='rsi_14', quantiles=10)\n",
    "        quantile_step_rsi14.run(factor_data=df_final)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 因子相关性分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if df_final is not None and not df_final.empty:\n",
    "    factor_corr = df_final[factor_cols].corr()\n",
    "\n",
    "    plt.figure(figsize=(10, 8))\n",
    "    sns.heatmap(factor_corr, annot=True, cmap='coolwarm', fmt='.2f')\n",
    "    plt.title('因子相关性矩阵')\n",
    "    plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}